<@layout.skeleton>

<@app.jsLink 'jquery.validate.js'/>
<@app.jsLink 'additional-methods.js'/>
<@app.jsLink 'validation-commons.js'/>
<@app.jsLink 'task.js'/>

<h2>
    <#if edit??>
        Edit task <a href="<@spring.url '/${currentProject.shortName}'/>">${currentProject.name}</a> / <a href="<@spring.url '/${currentProject.shortName}/${currentTask.shortName}'/>">${currentTask.name}</a>
    <#else>
        New task in <a href="<@spring.url '/${currentProject.shortName}'/>">${currentProject.name}</a>
    </#if>
</h2>
    <form id="form" class="form-horizontal">
        <div class="input-group control-group">
            <div class="control-group">
                <label for="branches-input-type" class="control-label">Branches</label>
                <div class="controls">
                    <select id="branches-input-type" name="branchesInputType" class="input-medium">
                        <option value="select">select</option>
                        <option value="input"
                            <#if edit?? && !areBranchesPresent(currentProject.branches, currentTask.urlFrom, currentTask.urlTo)>
                                selected="selected"
                            </#if>
                                >input</option>
                    </select>
                    <a id="refresh-branches-button" class="btn">Refresh</a>
                    <img id="indicator" src="<@spring.url '/resources/img/ajax-loader.gif'/>" class="input-append hide">
                </div>
            </div>

            <div id="branches-select" class="control-group">
                <div class="control-group">
                    <label for="url-from-select" class="control-label">Source</label>
                    <div class="controls">
                        <select id="url-from-select" name="urlFromSelect">
                            <#if edit??>
                                <@renderBranchOptions currentTask.urlFrom/>
                            <#else>
                                <@renderBranchOptions/>
                            </#if>
                        </select>
                        <label for="url-from-select" class="help-inline"></label>
                    </div>
                </div>
                <div class="control-group">
                    <label for="url-to-select" class="control-label">Destination</label>
                    <div class="controls">
                        <select id="url-to-select" name="urlToSelect">
                            <#if edit??>
                                <@renderBranchOptions currentTask.urlTo/>
                            <#else>
                                <@renderBranchOptions/>
                            </#if>
                        </select>
                        <label for="url-to-select" class="help-inline"></label>
                    </div>
                </div>
            </div>

            <div id="branches-input" class="control-group">
                <div class="control-group">
                    <label for="url-from-input" class="control-label">Source</label>
                    <div class="controls">
                        <input id="url-from-input" name="urlFromInput" path="urlFrom" type="text" class="input-block-level" placeholder="URL of source branch" maxlength="500"
                            <#if edit??>
                               value="${currentTask.urlFrom}" initial-value="${currentTask.urlFrom}"
                            </#if>
                                />
                        <label for="url-from-input" class="help-inline"></label>
                    </div>
                </div>
                <div class="control-group">
                    <label for="url-to-input" class="control-label">Destination</label>
                    <div class="controls">
                        <input id="url-to-input" name="urlToInput" path="urlTo" type="text" class="input-block-level" placeholder="URL of destination branch" maxlength="500"
                            <#if edit??>
                               value="${currentTask.urlTo}" initial-value="${currentTask.urlTo}"
                            </#if>
                                />
                        <label for="url-to-input" class="help-inline"></label>
                    </div>
                </div>
            </div>
        </div>

        <div class="input-group control-group">
            <div class="control-group">
                <label for="name" class="control-label">Name</label>
                <div class="controls">
                    <input id="name" name="name" path="name" type="text" class="input-block-level" placeholder="Task's name" maxlength="64"
                        <#if edit??>
                            value="${currentTask.name}" initial-value="${currentTask.name}"
                        </#if>
                            />
                    <label for="name" class="help-inline"></label>
                </div>
            </div>
            <div class="control-group">
                <label for="short-name" class="control-label">Short name</label>
                <div class="controls">
                    <input id="short-name" name="shortName" path="shortName" type="text" class="input-block-level" placeholder="Task's short name to be used in URLs" maxlength="32"
                        <#if edit??>
                           value="${currentTask.shortName}" initial-value="${currentTask.shortName}"
                        </#if>
                            />
                    <label for="short-name" class="help-inline"></label>
                </div>
            </div>
        </div>

        <div class="control-group">
            <label for="interval" class="control-label">Update interval, min</label>
            <#assign currentInterval = 15/>
            <#if edit??>
                <#assign currentInterval = currentTask.executionInterval/60 />
            </#if>
            <div class="controls">
                <input id="interval" name="interval" path="executionInterval" type="number" min="5" max="8640" step="5" class="input-mini" value="${currentInterval}">
                <label for="interval" class="help-inline"></label>
            </div>
        </div>

        <div class="controls">
            <button type="submit" class="btn btn-primary">
                <#if edit??>
                    Apply
                <#else>
                    Create
                </#if>
            </button>
            <#if edit??>
                <a class="btn" href="<@spring.url '/${currentProject.shortName}/${currentTask.shortName}?edit'/>">Reset</a>
            </#if>
        </div>
        <input id="project-short-name" name="projectShortName" type="hidden" value="${currentProject.shortName}"/>
        <#if edit??>
            <input id="task-short-name" name="task-short-name" type="hidden" value="${currentTask.shortName}"/>
        </#if>
    </form>

    <#if !edit??>
        <#-- modal -->
        <div id="ask-user-modal" class="modal hide">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h3>New task was successfully created</h3>
            </div>
            <div class="modal-body">
                <p>Do you want to add more tasks?</p>
            </div>
            <div class="modal-footer">
                <button id="add-task-button" class="btn btn-primary" data-dismiss="modal">Yes</button>
                <button id="modal-dismiss-button" class="btn" data-dismiss="modal">No</button>
            </div>
        </div>
        <#-- modal end -->
    </#if>

<#macro renderBranchOptions currentBranch=''>
    <#if currentProject.branches?size == 0>
        <option disabled="disabled">No branches</option>
    <#else>
        <#list currentProject.branches as branch>
            <option value="${branch.url}"
                <#if currentBranch?? && currentBranch?replace('/$', '', 'r') == branch.url>
                    selected="selected"
                </#if>
                    >${branch.name}</option>
        </#list>
    </#if>
</#macro>

<#function areBranchesPresent branches urlFrom, urlTo>
    <#assign sourceBranchPresent = false>
    <#assign targetBranchPresent = false>
    <#list branches as branch>
        <#if !sourceBranchPresent && branch.url == urlFrom?replace('/$', '', 'r')>
            <#assign sourceBranchPresent = true/>
        </#if>
        <#if !targetBranchPresent && branch.url == urlTo?replace('/$', '', 'r')>
            <#assign targetBranchPresent = true/>
        </#if>
        <#if sourceBranchPresent && targetBranchPresent>
            <#break/>
        </#if>
    </#list>
    <#return sourceBranchPresent && targetBranchPresent/>
</#function>

</@layout.skeleton>
